/**
 * Created by Administrator on 15-6-17.
 */
var Contextmenu = RichBase.extend({
	statics : {
		STATE : {
			"1" : {
				name : "预留",
				html : '<span class="sicon"><i class="iconfont" style="color:#cd3de2">&#xe68c;</i></span>'
			},
			"3" : {
				name : "已出售",
				html : '<span class="sicon"><i class="iconfont" style="color:#f7931e">&#xe68c;</i></span>'
			}
		}
	},
	EVENTS : {
		"click" : {
			".menuGroup .menuLi" : "onAreaMenuLiClick",
			"#setSeatWrap" : "onSetSeatWrapClick",
			"#contextMenuUl" : "onSetSeatWrapClick",
			"#modifySeatBtn" : "onModifySeatBtnClick",
			".menuLiState" : "onMenuStateClick"
		},
		"mouseenter" : {
			"#setVenueMenuLi" : "onSetVenueMenuLiMouseenter",
			".menuGroup" : "onMenuGroupMouseenter"

		},
		"mouseleave" : {
			"#setVenueMenuLi" : "onSetVenueMenuLiMouseleave",
			"#setSeatWrap" : "onSetSeatWrapMouseleave"
		},
		"focus" : {
			".setSeatInput" : "onInputFocus"
		},
		"blur" : {
			".setSeatInput" : "onInputBlur"
		}
	},
	init : function(opt){
		this.container = opt.container;
		this.stageWrap = $("#stageWrap");
		this.menuWrap = $("#contextMenuUl");
		this.setSeatWrap = $("#setSeatWrap");
	},
	//归为指定某个区 或设为不可用
	onAreaMenuLiClick : function(that,e){
		var tarMenu = $(e.currentTarget);
		var areaid = tarMenu.attr("data-areaid");
		var color = tarMenu.attr("data-color");
		var stageUl = $("#stageUl");
		stageUl.children(".selected").attr("data-areaid",areaid).attr("data-state","1").css({backgroundColor:"#"+color});
		stageUl.children().removeClass("selected");
		that.container.click();
	},
	onSetVenueMenuLiMouseenter : function(that,e){
		var tarLi = $(e.currentTarget);
		if(tarLi.hasClass("unable")) return false;
		var offset = tarLi.offset();
		var yoff = -50;
		var liW = that.menuWrap.width()+1;
		var scrollTop = $(window).scrollTop();
		var scrollLeft = $(window).scrollLeft();
		that.setSeatWrap.show().css({
			"left" : offset.left+liW-scrollLeft,
			"top" : offset.top+yoff-scrollTop
		})
	},
	onInputFocus : function(that,e){
		var tarInp = $(e.currentTarget);
		tarInp.attr("data-orign",tarInp.val());
	},
	onInputBlur : function(that,e){
		var tarInp = $(e.currentTarget);
		var val = tarInp.val();
		var orignVal = tarInp.attr("data-orign");
		var max = tarInp.attr("data-max");
		if(!val || !PFT_GLOBAL.U.isPositiveNum(val) || (Number(val)>Number(max))) tarInp.val(orignVal);
	},
	//设置坐位
	onModifySeatBtnClick : function(that,e){
		var newRow = $("#setSeat_row_input").blur().val();
		var newCol = $("#setSeat_col_input").blur().val();
		var tarStageBox = that.stageWrap.children(".stageUlW").children(".stageUl").children(".selected");
		if(tarStageBox.length){
			var seat = tarStageBox.attr("data-seat");
			var newSeat = newRow+"-"+newCol;
			if(seat!==newSeat) tarStageBox.attr("data-seat",newSeat).removeClass("selected").find(".t").text(newSeat);
		}
		that.container.click();
	},
	//设置坐位状态
	onMenuStateClick : function(that,e){
		var tarMenu = $(e.currentTarget);
		var state = tarMenu.attr("data-state");
		var stageUl = $("#stageUl");
		var STATE = that.statics.STATE;
		var html = STATE[state] ? STATE[state]["html"] : "";
		stageUl.children(".selected").attr("data-state",state).children(".state").html(html);
		stageUl.children().removeClass("selected");
		that.container.click();
	},
	onMenuGroupMouseenter : function(that,e){
		e.preventDefault();
		e.stopPropagation();
		that.setSeatWrap.hide();
	},
	onSetSeatWrapMouseleave : function(that,e){
		e.preventDefault();
		e.stopPropagation();
		that.setSeatWrap.hide();
	},
	onSetSeatWrapClick : function(that,e){
		e.stopPropagation();
		e.preventDefault();
	}

});

new Contextmenu({container:$("#contextMenuWrap")});